package com.atguigu.gmall.sms.service.impl;

import com.atguigu.gmall.sms.dto.SkuSalesDto;
import com.atguigu.gmall.sms.entity.SkuBoundsEntity;
import com.atguigu.gmall.sms.entity.SkuFullReductionEntity;
import com.atguigu.gmall.sms.service.SkuBoundsService;
import com.atguigu.gmall.sms.service.SkuFullReductionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.sms.mapper.SkuLadderMapper;
import com.atguigu.gmall.sms.entity.SkuLadderEntity;
import com.atguigu.gmall.sms.service.SkuLadderService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuLadderService")
public class SkuLadderServiceImpl extends ServiceImpl<SkuLadderMapper, SkuLadderEntity> implements SkuLadderService {

    @Autowired
    private SkuBoundsService skuBoundsService;
    @Autowired
    private SkuFullReductionService skuFullReductionService;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuLadderEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuLadderEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    @Transactional
    public void saveSkuSales(SkuSalesDto skuSalesDto) {

        SkuBoundsEntity skuBoundsEntity = new SkuBoundsEntity();
        skuBoundsEntity.setGrowBounds(new BigDecimal(skuSalesDto.getGrowBounds()));
        skuBoundsEntity.setBuyBounds(new BigDecimal(skuSalesDto.getBuyBounds().intValue()));
        skuBoundsEntity.setSkuId(skuSalesDto.getSkuId());
        Integer[] work = skuSalesDto.getWork();
        skuBoundsEntity.setWork(work[0]*1+work[1]*2+work[2]*4+work[3]*8);
        skuBoundsService.save(skuBoundsEntity);

        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setFullCount(skuSalesDto.getFullCount());
        skuLadderEntity.setDiscount(skuSalesDto.getDiscount());
        skuLadderEntity.setAddOther(skuSalesDto.getLadderAddOther());
        skuLadderEntity.setSkuId(skuSalesDto.getSkuId());
        this.save(skuLadderEntity);
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        skuFullReductionEntity.setFullPrice(skuSalesDto.getFullPrice());
        skuFullReductionEntity.setReducePrice(skuSalesDto.getReducePrice());
        skuFullReductionEntity.setAddOther(skuSalesDto.getFullAddOther());
        skuFullReductionEntity.setSkuId(skuSalesDto.getSkuId());
        skuFullReductionService.save(skuFullReductionEntity);
    }

}